package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/06 09:02
 **/
public class Day4_1_Num828_UniqueLetterString {
    class Solution {
        public int uniqueLetterString(String s) {
            int n = s.length();
            long ans = 0;
            char[] ch = s.toCharArray();
            for (char i = 'A'; i <= 'Z'; i++) {
                for (int j = 0, l = -1, r = -1; j < n; ++j) {
                    if (ch[j] == i) {l = r; r = j;}
                    ans += r - l;
                }
            }
            return (int)(ans % 1000000007);
        }
    }
    class Solution2 {
        public int uniqueLetterString(String s) {
            int n = s.length();
            char[] ch = s.toCharArray();
            int res = 0;
            for(int i = 0; i < n; ++i){
                int left = i-1, right = i+1;
                while(left >= 0 && ch[left] != ch[i]) left--;
                while(right < n && ch[right] != ch[i]) right++;
                res += (i-left) * (right-i);
            }
            return res;
        }
    }
}